package me.dwliu.lab.demo.sso.auth.service;

import lombok.AllArgsConstructor;
import me.dwliu.lab.demo.sso.auth.dao.UserDao;
import me.dwliu.lab.demo.sso.auth.entity.UserEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户Service
 *
 * @author liudw
 * @date 2020/6/27 15:01
 **/
@Service
@AllArgsConstructor
public class CustomUserDetailsServiceImpl implements UserDetailsService {

    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserEntity userEntity = userDao.getByUsername(username);


        //封装权限标识
        Set<String> permsSet = new HashSet<>();
        permsSet.add("admin");
        Set<GrantedAuthority> authorities = permsSet
                .stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());


        User userDetails = new User(username, userEntity.getPassword(), authorities);
        return userDetails;
    }
}
